package com.example.carrierapp.ui.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;

import com.example.lb.fingerdemo.R;

/**
 *
 */
public class MainNavItemView extends AppCompatTextView implements Checkable {
    private boolean mIsChecked;

    private Drawable mIcon;
    private ColorStateList mColorStateList;
    private boolean mShowRedIcon = false;
    private Drawable mRedIconDrawable;

    public MainNavItemView(Context context) {
        this(context, null, 0);
    }

    public MainNavItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MainNavItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MainNavItemView);
//        mIcon = a.getDrawable(R.styleable.MainNavItemView_icon);
//        assert mIcon != null;
//        mIcon.setState(View.ENABLED_STATE_SET);
//        setCompoundDrawables(null, mIcon.getCurrent(), null, null);
//        a.recycle();
        init();
    }

    private void init() {
        mColorStateList = getTextColors();
        mIcon = getCompoundDrawables()[1];
        mRedIconDrawable = getResources().getDrawable(R.drawable.unread_count_bg);
    }

    public void setIcon(Drawable icon) {
        mIcon = icon;
    }

    public void setIcon(@DrawableRes int drawableRes) {
        mIcon = getResources().getDrawable(drawableRes);
    }

    @Override
    public void setChecked(boolean checked) {
        if (checked) {
            if (mIcon != null) {
                mIcon.setState(View.PRESSED_WINDOW_FOCUSED_STATE_SET);
                internalSetCompoundDrawables(null, mIcon.getCurrent(), null, null);
            }
            int color = mColorStateList.getColorForState(View.PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET, Color.BLACK);
            super.setTextColor(color);
        } else {
            if (mIcon != null) {
                mIcon.setState(View.ENABLED_STATE_SET);
                internalSetCompoundDrawables(null, mIcon.getCurrent(), null, null);
            }
            super.setTextColor(mColorStateList);
        }
        mIsChecked = checked;
    }

    public void setShowRedIcon(boolean showRedIcon) {
        if (mShowRedIcon == showRedIcon) {
            return;
        }
        mShowRedIcon = showRedIcon;
        invalidate();
    }

    private void internalSetCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
        super.setCompoundDrawables(left, top, right, bottom);
    }

    @Override
    public void setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
        mIcon = top;
        if (mIcon != null && mColorStateList != null) {
            setChecked(mIsChecked);
        } else {
            super.setCompoundDrawables(left, top, right, bottom);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mShowRedIcon) {
            int iconW = mIcon.getIntrinsicWidth();
            int w = canvas.getWidth();
            int left = w/2 + iconW/2 - mRedIconDrawable.getIntrinsicWidth() /2;
            int right = left + mRedIconDrawable.getIntrinsicWidth();
            int top = 0;
            int bottom = top + mRedIconDrawable.getIntrinsicHeight();
            mRedIconDrawable.setBounds(left, top, right, bottom);
            mRedIconDrawable.draw(canvas);
        }
    }

    @Override
    public void setTextColor(int color) {
        super.setTextColor(color);
        mColorStateList = ColorStateList.valueOf(color);
    }

    @Override
    public void setTextColor(ColorStateList colors) {
        super.setTextColor(colors);
        mColorStateList = colors;
    }

    @Override
    public boolean isChecked() {
        return mIsChecked;
    }

    @Override
    public void toggle() {
        setChecked(!mIsChecked);
    }
}
